// 导入模块 Import modules
import { Administrators } from './admin/admin';
import { admin } from './admin/message';
import { PlayerManager } from './manager/player';

// 清空控制台 Clear the console
console.clear();

// 玩家进入地图 Player join world
world.onPlayerJoin(async ({ entity }) => {
    entity.player.enableDoubleJump = false;
    await PlayerManager.create(entity).initData();
});
// 玩家离开地图 Player leave world
world.onPlayerLeave(async ({ entity }) => {
    await PlayerManager.create(entity).saveData();
});

// 监测玩家右键事件 Monitor player right button event
world.onPress(async ({ entity, button }) => {
    if (button === GameButtonType.ACTION1) {
        const bag = await entity.player.dialog<GameSelectDialogParams>({
            type: GameDialogType.SELECT,
            title: '玩家面板',
            content: '\n',
            options: ['保存'],
        });
        if (!bag) {
            return;
        } else if (bag.value === '保存') {
            await PlayerManager.create(entity).saveData();
        }
    }
});

// 管理员权限 Administrators permission
/**
 * @zh 玩家是否为管理员
 * @param entity 玩家实体
 * @returns 返回玩家是否为管理员
 * @en Check if the player is administrator
 * @param entity Player entity
 * @returns Returns boolean to show whether the player is administrator
 */
function isAdministrator(entity: GamePlayerEntity): boolean {
    for (let i = 0; i < Administrators.length; i++) {
        if (Administrators[i].userId === entity.player.userId) {
            return true;
        }
    }
    return false;
}
world.onChat(({ entity, message }) => {
    if (entity.player || entity.isPlayer) {
        if (isAdministrator(entity as GamePlayerEntity) && (
            message.startsWith("/") || message.startsWith("\\")
        )) {
            const value = admin(entity as GamePlayerEntity, message);
            entity.player?.directMessage(value);
        }
    }
});
